import React from "react";
import { getNewsById } from '../../apiList/api'
import { parseTime } from '../../utils/jcore/time'

export interface IFNewsItem {
    id: string;
    title: string;
    cover: string;
    content: string;
    createTime: number;
}

type P = {
    id: string
    closeNewsInfo?: () => void;
}

type S = {
    newsInfo: IFNewsItem;
}

export default class NewsDetail extends React.Component<P, S> {
    constructor(props: P) {
        super(props)
        this.state = {
            newsInfo: {
                id: '',
                title: '',
                cover: '',
                content: '',
                createTime: 0
            }
        }
    }
    UNSAFE_componentWillMount() {
        this.getNewsById()
    }
    getNewsById() {
        let id = this.props.id
        if (!id) {
            return console.info('没有新闻id')
        }
        getNewsById({ id }).then(res => {
            this.setState({ newsInfo: res })
        })
    }
    closeNewsInfo = () => {
        if (this.props.closeNewsInfo) {
            this.props.closeNewsInfo()
        }
    }
    render() {
        const news = this.state.newsInfo
        return (
            <div>
                <p>id-{news.id}</p>
                <h1>标题-{news.title}</h1>
                <p>详细信息-{news.content}</p>
                <p>时间: {parseTime(news.createTime)}</p>
                <img style={{ maxWidth: "100%" }} src={news.cover} alt="err" />
            </div>
        )
    }
}